package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import org.jdesktop.swingx.JXHyperlink;
import org.jdesktop.swingx.JXTextField;

import controller.LogginController;

import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

public class PanelLoggin extends JPanel {
	private static final long serialVersionUID = -1952081005312416343L;
	private JXTextField user;
	private JPasswordField pass;
	private LogginController control;
	private MainView mainView;
	private JButton btnLoggin;
	private JXHyperlink btnCuHnhH;

	public PanelLoggin(MainView mainView) {
		this.mainView = mainView;
		this.control = new LogginController(this);
		setLayout(new BorderLayout(0, 0));

		JPanel mainPanel = new JPanel();
		mainPanel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "", TitledBorder.CENTER,
				TitledBorder.TOP, null, new Color(51, 153, 255)));
		add(mainPanel);
		mainPanel.setLayout(new GridLayout(2, 1, 0, 0));

		JPanel topPanel = new JPanel();
		mainPanel.add(topPanel);
		topPanel.setLayout(new BorderLayout(0, 0));

		JPanel userPanel = new JPanel();
		topPanel.add(userPanel, BorderLayout.SOUTH);

		JLabel lblUser = new JLabel("Tài khoản");
		lblUser.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblUser.setIcon(new ImageIcon(PanelLoggin.class
				.getResource("/images/user.png")));
		userPanel.add(lblUser);

		user = new JXTextField();
		user.setPrompt("Nhập tên tài khoản");
		user.setActionCommand("key");
		user.addActionListener(control);
		user.addFocusListener(control);
		userPanel.add(user);
		user.setColumns(18);

		JPanel bottPanel = new JPanel();
		mainPanel.add(bottPanel);
		bottPanel.setLayout(new BorderLayout(0, 18));

		JPanel passPanel = new JPanel();
		bottPanel.add(passPanel, BorderLayout.NORTH);

		JLabel lblPass = new JLabel("Mật khẩu");
		lblPass.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblPass.setIcon(new ImageIcon(PanelLoggin.class
				.getResource("/images/key.png")));
		passPanel.add(lblPass);

		pass = new JPasswordField();
		pass.setActionCommand("pass");
		pass.addActionListener(control);
		pass.addFocusListener(control);
		passPanel.add(pass);
		pass.setColumns(18);

		JPanel btnPanel = new JPanel();
		bottPanel.add(btnPanel, BorderLayout.CENTER);

		btnLoggin = new JButton("Đăng nhập");
		btnLoggin.setActionCommand("loggin");
		btnLoggin.addActionListener(control);
		btnLoggin.setFocusable(false);
		btnLoggin.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnLoggin.setIcon(new ImageIcon(PanelLoggin.class
				.getResource("/images/locked.png")));
		btnPanel.add(btnLoggin);

		JButton btnExit = new JButton("Thoát");
		btnExit.setFocusable(false);
		btnExit.setActionCommand("exit");
		btnExit.addActionListener(control);
		btnExit.setFont(new Font("Tahoma", Font.PLAIN, 15));
		btnExit.setIcon(new ImageIcon(PanelLoggin.class
				.getResource("/images/cancel.png")));
		btnExit.setPreferredSize(btnLoggin.getPreferredSize());
		btnPanel.add(btnExit);
		lblPass.setPreferredSize(lblUser.getPreferredSize());

		JPanel version = new JPanel();
		version.setBorder(new EmptyBorder(10, 3, 9, 3));
		add(version, BorderLayout.SOUTH);
		version.setLayout(new BorderLayout(0, 0));

		JLabel lblVer = new JLabel("Phiên bản 1.0 - 2014");
		version.add(lblVer, BorderLayout.WEST);
		
		btnCuHnhH = new JXHyperlink();
		btnCuHnhH.setClickedColor(Color.BLUE);
		btnCuHnhH.setFont(new Font("Tahoma", Font.PLAIN, 13));
		btnCuHnhH.setText("Tùy chỉnh cài đặt");
		btnCuHnhH.setActionCommand("config");
		btnCuHnhH.addActionListener(control);
		btnCuHnhH.setFocusable(false);
		version.add(btnCuHnhH, BorderLayout.EAST);
		btnCuHnhH.setIcon(new ImageIcon(PanelLoggin.class.getResource("/images/advan.png")));
		btnCuHnhH.setVerticalAlignment(SwingConstants.TOP);
	}

	public void notifyShow() {
		mainView.show();
	}

	public String getUser() {
		return this.user.getText().toLowerCase();
	}

	public char[] getPass() {
		return this.pass.getPassword();
	}

	public void setText() {
		this.pass.setText("");
		this.pass.requestFocus();
	}

	public JTextField getTxtUser() {
		return this.user;
	}

	public JPasswordField getPassField() {
		return this.pass;
	}

	public JButton getBtnLoggin() {
		return this.btnLoggin;
	}
	
	public JXHyperlink getBtnConfig() {
		return this.btnCuHnhH;
	}

}
